package service;

import entity.EntityContext;
import entity.ExamInformation;
import entity.QuestionInfo;
import entity.User;
import ui.ClientContext;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/*
业务判断
*/
public class ContentContext {
    private ClientContext client;
    private EntityContext entity;


    public User loginUser() throws IdOrPwdException {
        //ArrayList<User> user= entity.UserResolve();
        /*for (User u:user) {
            if (u.getId()==client.getId()&&u.getPassword().equals(client.getPwd())){
                return u;
            }
        }*/
        Map<Integer,User> user=entity.getUserMap();
        if (user.get(client.getId()) != null && user.get(client.getId()).getPassword().equals(client.getPwd())) {
            entity.setUser(user.get(client.getId()));
            return user.get(client.getId());
        }
        else {
            throw new IdOrPwdException("账号或密码错误！");
        }

        //return user.get(client.getId());
    }


    public void setClient(ClientContext client) {
        this.client = client;
    }

    public void setEntity(EntityContext entity) {
        this.entity = entity;
    }

    //获得题库
    public QuestionInfo start() throws IOException {
       /*1.生成试卷
        2.开始考试
        3.上一题
        4.下一题
        5.交卷
        6.倒计时*/
        //生成试卷
        /*entity.createPaper();
        Map<Integer, QuestionInfo> paper= entity.getPaper();*/
        entity.newCount();
        entity.setScore(0);
        entity.createLevelPaper();
        //entity.createPaper();
        Map<Integer, QuestionInfo> paper=entity.getPaper();
        return paper.get(1);
    }

    public String getRule() {
        return entity.getRule();
    }

    public QuestionInfo nextQues() {
        if (entity.getCount()<20){
            entity.countAdd();
            Map<Integer, QuestionInfo> paper= entity.getPaper();
            return paper.get(entity.getCount());
        }
        else {
            Map<Integer, QuestionInfo> paper= entity.getPaper();
            client.nextTitle();
            return paper.get(entity.getCount());
        }

    }
    public QuestionInfo prevQues() {
        if (entity.getCount()>1){
            entity.countDecrease();
            Map<Integer, QuestionInfo> paper= entity.getPaper();
            return paper.get(entity.getCount());
        }
        else {
            client.prevTitle();
            Map<Integer, QuestionInfo> paper= entity.getPaper();
            return paper.get(entity.getCount());
        }

    }


    public void subAnswer(List<Integer> userAnswer) {
        entity.subAnswer(userAnswer);
    }

    //如果为空
    public void countScore() {
        int score=0;
        Map<Integer, List<Integer>> paperAnswer = entity.getPaperAnswer();
        Map<Integer, QuestionInfo> paper = entity.getPaper();
        for (int i = 1; i <= paperAnswer.size(); i++) {
            if (paper.get(i).getUserAnswers()!=null&&paperAnswer.get(i).size()==paper.get(i).getUserAnswers().size()){
                for (int j = 0; j < paperAnswer.get(i).size(); j++) {
                    if (paperAnswer.get(i).get(j)==paper.get(i).getUserAnswers().get(j)){
                        score+=5;
                    }
                }
            }
        }
        entity.setScore(score);
    }
    public int getScore() {
        return entity.getScore();
    }


    public ExamInformation getInformation() {
        return entity.getInformation();
    }
}
